package com.demo6.controller;

import com.demo6.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

//@RestController=@Controller+@ResponseBody显示不出来页面 只能显示字符
//@ResponseBody显示页面的内容
@Controller
@RequestMapping("/tm")
public class tmcontroller {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello springboot-1";
    }
    //1
//    @RequestMapping("/toindex")
//    public String toindex(Map<String,Object> map){
//        map.put("name","李四");
//        map.put("age","20");
//        map.put("sno","001");
//        return "index";
//    }
    //2
//    @RequestMapping("/toindex")
//    public ModelAndView toindex(){
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("name","张三");
//        mv.addObject("age","21");
//        mv.addObject("sno","002");
//        mv.addObject("isstudent",false);//为true是显示学生 为false时不显示学生
//        mv.setViewName("index");
//        return mv;
//    }
    @RequestMapping("/toindex")
    public ModelAndView toindex(){
        ModelAndView mv = new ModelAndView();
        //创建学生对象
        Student stu=new Student();
        stu.setSno("006");
        stu.setSname("Jerry");
        stu.setSage(23);
        stu.setSgender("男");
        mv.addObject("stuinfo",stu);
        mv.setViewName("index");
        return mv;
    }
    //登陆页面
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }
    //RequestParam 请求的参数
    //验证账号和密码是否正确
    @RequestMapping("/cheak")
    public String cheak(@RequestParam("username") String username, @RequestParam("password") String password){
        if (username.equals("admin") && password.equals("123")){
            //验证通过
            return "redirect:toindex";
        }else {
            //验证未通过
            return "redirect:tologin";
        }
    }
}
